package com.mazaiting.cryption.sha;

import org.bouncycastle.crypto.digests.*;
import org.bouncycastle.util.encoders.Hex;

/**
 * SHA 签名算法
 *
 * @author mazaiting
 * @create_time 2022/4/8 23:27
 */
public class ShaUtil {

    /**
     * sha 1 散列
     *
     * @param data 待签名数据
     * @return 签名后数据
     */
    public static String sha1(String data) {
        SHA1Digest digester = new SHA1Digest();
        byte[] retValue = new byte[digester.getDigestSize()];
        digester.update(data.getBytes(), 0, data.length());
        digester.doFinal(retValue, 0);
        return Hex.toHexString(retValue).toUpperCase();
    }

    /**
     * sha 224 散列
     *
     * @param data 待签名数据
     * @return 签名后数据
     */
    public static String sha224(String data) {
        SHA224Digest digester = new SHA224Digest();
        byte[] retValue = new byte[digester.getDigestSize()];
        digester.update(data.getBytes(), 0, data.length());
        digester.doFinal(retValue, 0);
        return Hex.toHexString(retValue).toUpperCase();
    }

    /**
     * sha 256 散列
     *
     * @param data 待签名数据
     * @return 签名后数据
     */
    public static String sha256(String data) {
        SHA256Digest digester = new SHA256Digest();
        byte[] retValue = new byte[digester.getDigestSize()];
        digester.update(data.getBytes(), 0, data.length());
        digester.doFinal(retValue, 0);
        return Hex.toHexString(retValue).toUpperCase();
    }

    /**
     * sha 384 散列
     *
     * @param data 待签名数据
     * @return 签名后数据
     */
    public static String sha384(String data) {
        SHA384Digest digester = new SHA384Digest();
        byte[] retValue = new byte[digester.getDigestSize()];
        digester.update(data.getBytes(), 0, data.length());
        digester.doFinal(retValue, 0);
        return Hex.toHexString(retValue).toUpperCase();
    }

    /**
     * sha 512 散列
     *
     * @param data 待签名数据
     * @return 签名后数据
     */
    public static String sha512(String data) {
        SHA512Digest digester = new SHA512Digest();
        byte[] retValue = new byte[digester.getDigestSize()];
        digester.update(data.getBytes(), 0, data.length());
        digester.doFinal(retValue, 0);
        return Hex.toHexString(retValue).toUpperCase();
    }
}
